package com.yanqu.road.server.cmd.activity.snowgarden;

import com.yanqu.road.entity.activity.snowgarden.SnowGardenUserData;
import com.yanqu.road.entity.activity.snowgarden.config.SnowGardenConfig;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.SnowGardenProto;
import com.yanqu.road.pb.player.PlayerProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.snowgarden.SnowGardenModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.snowgarden.SnowGardenMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Cmd(code = GameProtocol.S_SNOW_GARDEN_GET_RECORD_USER_INFO_FROM_CROSS,desc = "获取跨服上的玩家信息 用来返回拜访记录列表给客户端")
public class SnowGetOutVisitUserFromCrossCmd extends AbstractCommand {
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        SnowGardenConfig config = SnowGardenMgr.getSnowGardenConfig();
        if (config == null || !ActivityMgr.activityInTime(config.getActivityInfo())) {
            ret = GameErrorCode.E_ACTIVITY_HAD_END;
            return;
        }
        SnowGardenProto.SnowGardenGetQuitUserRespMsg snowGardenGetQuitUserRespMsg = SnowGardenProto.SnowGardenGetQuitUserRespMsg.parseFrom(packet.getBytes());
        Map<Long,UserBaseInfo> baseInfoMap = new HashMap<>();
        for (SnowGardenProto.SnowPlayerIdAndData msg:snowGardenGetQuitUserRespMsg.getDataList()){
            UserBaseInfo baseInfo = PlayerBasePb.parseBaseUserInfo(msg.getUserData());
            baseInfoMap.put(msg.getUserId(), baseInfo);
        }

        ret = player.getModule(SnowGardenModule.class).getVisitRecordListMsg(snowGardenGetQuitUserRespMsg.getType(),true,baseInfoMap);

    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            SnowGardenProto.SnowGardenVisitListRespMsg.Builder builder = SnowGardenProto.SnowGardenVisitListRespMsg.newBuilder();
            builder.setRet(ret);
            player.sendPacket(ClientProtocol.U_SNOW_GARDEN_GET_VISITOR_RECORD_LIST, builder);
        }
    }
}
